package org.egrit.util;

import java.util.List;
import java.util.Map;


/**
 * Provides static utility methods for common argument checking tasks.
 * 
 * @author michael
 */
public class Args {
    
    /**
     * Checks if the input value is null and, if so, throws an exception with
     * the var name in the message.
     * 
     * @param varName name to include in exception message if null is detected
     * @param varValue value to check for null
     */
    public static void notNull(String varName, Object varValue) {
        if (varValue == null) {
            throw new IllegalArgumentException(varName + " must not be null");
        }
    }

    /**
     * Performs checks for null on the input array of name (String) : object pairs.
     * 
     * @param args name : object pairs
     */
    public static void notNull(Object ...args) {
        if (args.length % 2 != 0) {
            throw new IllegalArgumentException("Input args must be String:Object pairs");
        }
        
        for (int i = 0; i < args.length; i += 2) {
            notNull((String) args[i], args[i+1]);
        }
    }

    public static void stringNotNullOrEmpty(String varName, String varValue) {
        if (varValue == null || varValue.trim().length() == 0) {
            throw new IllegalArgumentException(varName + " must not be null or empty");
        }
    }

    public static <T> void arrayNotNullOrEmpty(String varName, T[] array) {
        if (array == null || array.length == 0) {
            throw new IllegalArgumentException(varName + " must not be null or empty");
        }
    }
    
    public static void collectionNotNullOrEmpty(String varName, Map varValue) {
        if (varValue == null || varValue.isEmpty()) {
            throw new IllegalArgumentException(varName + " must not be null or empty");
        }
    }

    public static void collectionNotNullOrEmpty(String varName, List varValue) {
        if (varValue == null || varValue.isEmpty()) {
            throw new IllegalArgumentException(varName + " must not be null or empty");
        }
    }

    /*
    public static Object[] wrapAsObjectArray(Object ...params) {
        return params;
    }
    * 
    */

}
